<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2017-09-07
 * Time: 11:47
 */
namespace app\common\model;
use think\Model;

class UserApi extends Model{

    public function wxapisave($data,$tj){
        $array = array(
          'uapid' => $data['openid'],
          'username' => $this->saname($data['nickname']),
          'uniname' => $data['nickname'],
          'usex' => $data['sex'],
          'uicon' => $data['headimgurl'],
        );
        $data['addtime'] = time();
        $data['endtime'] = time();
        $data['addip'] = request()->ip();
        $data['conf'] = serialize($data);
        $data['uid'] = \model('user')->wxapiadd($array);
        $this->save($data);
        if($tj != false){
            model('UserSpread')->add_tui($data['uid'],$tj);
        }
        return $data;
    }
    //  生成随机用户名
    protected function saname($res){
        $rands = 'WX'.substr(md5($res.time()),0,8);
        $uid = \model('user')->where('username',$rands)->value('uid');
        if(!empty($uid)){
            $this->saname($res);
        }
        return $rands;
    }
}